package com.yzw.hello.service.system.role.impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yzw.hello.model.dao.jpa.system.roleDao.RoleDao;
import com.yzw.hello.model.dao.mybatis.system.userMapper.UserMapper;
import com.yzw.hello.model.pojos.system.role.Role;
import com.yzw.hello.model.pojos.system.user.User;
import com.yzw.hello.model.pojos.utls.AjaxMessage;
import com.yzw.hello.model.vo.system.user.UserCheckedList;
import com.yzw.hello.service.system.role.RoleService;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;

	@Autowired
	private UserMapper userMapper;

	@Override
	public List<Role> getList() {
		// TODO Auto-generated method stub
		return (List<Role>) roleDao.findAll();
	}

	@Override
	public Role getOne(Integer id) {
		// TODO Auto-generated method stub
		return roleDao.findOne(id);
	}
	
	@Override
	public Set<UserCheckedList> getCheckedList(Integer id){
		// TODO Auto-generated method stub
		return userMapper.getCheckedList(id);
	} 

	@Override
	public AjaxMessage saveRoleUsers(Integer rid, String strUserIds) {
		AjaxMessage ajaxMessage = new AjaxMessage();
		String[] userIds = strUserIds.split(",");
		Role role = roleDao.findOne(rid);
		Set<User> users = userMapper.findByIds(userIds);
		role.setUsers(users);
		ajaxMessage.setStatus( roleDao.save(role));
		if (ajaxMessage.isStatus()) {
			ajaxMessage.setMessage("修改成功");
		}
		return ajaxMessage;
	}

}
